﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace VIP_Shop.GUI
{
    public partial class CTHoaDon : Form
    {
       VIP_Shop.Access_Data.AccessData data = new Access_Data.AccessData();
        DataSet dataSet;
        BindingSource bindSourceHDChiTiet;
        BindingSource bindSourceHoaDon;
        DataTable tableHoaDon;
        DataTable datatable = new DataTable();
               
        public CTHoaDon()
        {
            InitializeComponent();
            
        }

        private void CTHoaDon_Load(object sender, EventArgs e)
        {
            dataSet = new DataSet("hoadonbanhang");
            DataTable tableKhachHang = data.get_Datatable1("select * from khachhang");
            tableHoaDon = data.get_Datatable1("select * from hoadon");
            DataTable tableHDChiTiet = data.get_Datatable2("select * from hoadonchitiet");
            dataSet.Tables.Add(tableHoaDon);
            dataSet.Tables.Add(tableHDChiTiet);
            dataSet.Tables[0].TableName = "hoadon";
            dataSet.Tables[1].TableName = "hoadonchitiet";

            dataSet.Relations.Add(new DataRelation("HOADON_HOADONCHITIET",
                tableHoaDon.Columns["mahd"],
                tableHDChiTiet.Columns["mahd"]));
            bindSourceHoaDon =
                new BindingSource(dataSet, "hoadon");
            bindSourceHDChiTiet =
                new BindingSource(bindSourceHoaDon, "HOADON_HOADONCHITIET");

            this.comboBox1.DataSource = tableKhachHang;
            this.comboBox1.DisplayMember = "hoten";
            this.comboBox1.ValueMember = "makh";
            this.dataGridView1.DataSource = bindSourceHDChiTiet;

            this.textBox1.DataBindings.Add("Text", bindSourceHoaDon, "mahd");
            this.maskedTextBox1.DataBindings.Add("Text", bindSourceHoaDon,
                "hanghd", true, DataSourceUpdateMode.OnValidation, null, "MM/dd/yyyy");
            this.comboBox1.DataBindings.Add("SelectedValue", bindSourceHoaDon, "makh");
            this.bindingNavigator2.BindingSource = bindSourceHoaDon; ;
            this.bindingNavigator1.BindingSource = bindSourceHDChiTiet;
         }
       
        
        private void button10_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.MoveNext();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.MovePrevious();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.MoveFirst();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.MoveLast();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.AddNew();
                     
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.AddNew();
            
        }

        private void button13_Click(object sender, EventArgs e)
        {
            this.bindSourceHoaDon.RemoveAt(this.bindSourceHoaDon.Position);
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.MoveNext();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.MoveLast();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.MovePrevious();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.MoveFirst();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.bindSourceHDChiTiet.RemoveAt(this.bindSourceHDChiTiet.Position);
           
        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
                bindSourceHoaDon.EndEdit();
               // data.UpdateView1(dataSet.Tables["HOADON"]);
                data.UpdateView1(dataSet.Tables[0]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                this.bindSourceHDChiTiet.EndEdit();
               // data.UpdateView1(dataSet.Tables["HOADONCHITIET"]);
                data.UpdateView1(dataSet.Tables[1]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
                
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.dateTimePicker1.CustomFormat = "dd/MM/yyyy";
            this.dateTimePicker1.Format =
                DateTimePickerFormat.Custom;
            this.maskedTextBox1.Text = this.dateTimePicker1.Text;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            try
            {
                bindSourceHoaDon.EndEdit();
                data.UpdateView1(dataSet.Tables["HOADONCHITIET"]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                bindSourceHDChiTiet.EndEdit();
                data.UpdateView(dataSet.Tables["hoadon"]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            try
            {
                bindSourceHDChiTiet.EndEdit();
                data.UpdateView(dataSet.Tables["HOADONCHITIET"]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            try
            {
                bindSourceHoaDon.EndEdit();
                data.UpdateView(dataSet.Tables["HOADONCHITIET"]);
            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            try
            {
                string mahd = textBox1.Text;
                string ngayhd = maskedTextBox1.Text;
                string makh = comboBox1.SelectedValue.ToString();
                data.Update("update hoadon set "
                            + "mahd='" + mahd+ "',"
                            + "hanghd='" +ngayhd + "',"
                            + "makh='" + makh + "',"
                            + "where mahd='" + mahd + "'");

            }
            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }
        }

       

               

      
    }

}
